package com.nm.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * Created by zhangyaosheng on 2018/5/11.
 */
//@RestController
public class loginSetSession {
    @RequestMapping("/session")
    public int setSession(String phone, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("user",phone);
        session.setMaxInactiveInterval(30 * 60);
        int flg=500;
        if(phone.equals(session.getAttribute("user"))){
            flg=200;
        }
        return flg;
    }
    @RequestMapping("/adminSession")
    public int setAdminSession(String flging, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("admin",flging);
        session.setMaxInactiveInterval(30 * 60);
        int flg=500;
        if(flging.equals(session.getAttribute("admin"))){
            flg=200;
        }
        return flg;
    }
    @RequestMapping("/cleanSession")
    public int clean(HttpServletRequest request){
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        int flg=500;
        if("".equals(em)||!em.hasMoreElements()){
            flg=200;
        }
        return flg;
    }
}
